//
//  WXPIncreaseQuotaPopView.m
//  MexicoLoan
//
//  Created by WXP on 2025/3/30.
//
//

#import "WXPIncreaseQuotaPopView.h"

@interface WXPIncreaseQuotaPopView ()

@end

@implementation WXPIncreaseQuotaPopView

#pragma mark - lazy
- (UIView *)viewBack
{
    if (_viewBack == nil) {
        _viewBack = [UIView  new];
        _viewBack.width = SCREEN_WIDTH-LEFT_MARDIN*2;
        _viewBack.backgroundColor = COLOR_WHITE;
        _viewBack.userInteractionEnabled = true;
        [GlobalMethod setRoundView:_viewBack color:[UIColor clearColor] numRound:16 width:0];
    }
    return  _viewBack;
}- (UIImageView *)imageBack
{
    if (_imageBack == nil) {
        _imageBack = [UIImageView  new];
        _imageBack.widthHeight = XY(self.viewBack.width, self.viewBack.width);
        _imageBack.image = [UIImage imageNamed:@"icon_home_increase_back"];
    }
    return  _imageBack;
}
- (UIImageView *)imageBig
{
    if (_imageBig == nil) {
        _imageBig = [UIImageView  new];
        _imageBig.widthHeight = XY(234, 228);
        _imageBig.image = [UIImage imageNamed:@"icon-home-increase-quota"];
    }
    return  _imageBig;
}
- (UILabel *)labelTitle
{
    if (_labelTitle == nil) {
        _labelTitle = [UILabel  new];
        _labelTitle.numberOfLines = 0;
        _labelTitle.lineSpace = 3;
        _labelTitle.textAlignment = NSTextAlignmentCenter;
        _labelTitle.textColor = COLOR_BLACK;
        _labelTitle.fontNum = 20;
        [_labelTitle fitTitle:@"¡Felicidades por mantener su\npalabra!" variable:0];
    }
    return  _labelTitle;
}
- (UIButton *)btnConfirm
{
    if (_btnConfirm == nil) {
        _btnConfirm = [UIButton  new];
        _btnConfirm.widthHeight = XY(self.viewBack.width-30-30, BUTTON_HEIGHT);
        [GlobalMethod setRoundView:_btnConfirm color:[UIColor clearColor] numRound:BUTTON_HEIGHT/2 width:0];
        _btnConfirm.backgroundColor = COLOR_PRIMARY;
        [_btnConfirm setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        _btnConfirm.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        [_btnConfirm setTitle:@"OK" forState:UIControlStateNormal];
        [_btnConfirm addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _btnConfirm;
}
- (UILabel *)labelMoneyLeft{
    if (!_labelMoneyLeft) {
        _labelMoneyLeft = [UILabel new];
        [GlobalMethod setLabel:_labelMoneyLeft widthLimit:0 isBold:true fontNum:18 textColor:COLOR_BLACK text:@""];
    }
    return _labelMoneyLeft;
}
- (UILabel *)labelMoneyMiddle{
    if (!_labelMoneyMiddle) {
        _labelMoneyMiddle = [UILabel new];
        [GlobalMethod setLabel:_labelMoneyMiddle widthLimit:0 isBold:true fontNum:18 textColor:COLOR_BLACK text:@""];
    }
    return _labelMoneyMiddle;
}
- (UILabel *)labelMoneyRight{
    if (!_labelMoneyRight) {
        _labelMoneyRight = [UILabel new];
        [GlobalMethod setLabel:_labelMoneyRight widthLimit:0 isBold:true fontNum:18 textColor:COLOR_WHITE text:@""];
    }
    return _labelMoneyRight;
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.widthHeight = XY(SCREEN_WIDTH, SCREEN_HEIGHT);
        self.backgroundColor = [UIColor colorWithHexString:@"000000" alpha:0.4];
        self.userInteractionEnabled = true;
        [self addSubView];
    }
    return self;
}

//添加subview
- (void)addSubView{
    [self addSubview:self.viewBack];
    [self.viewBack addSubview:self.imageBack];
    [self.viewBack addSubview:self.labelTitle];
    [self.viewBack addSubview:self.imageBig];
    [self.viewBack addSubview:self.btnConfirm];
    
    [self.imageBig addSubview:self.labelMoneyLeft];
    [self.imageBig addSubview:self.labelMoneyMiddle];
    [self.imageBig addSubview:self.labelMoneyRight];
    
    //初始化页面
    [self resetViewWithLeftMoney:@"60" middleMoney:@"" rightMoney:@"" time:60];
}

#pragma mark - 点击事件
- (void)btnClick:(UIButton *)sender{
    if(self.blockOKClick){
        self.blockOKClick();
    }
}

- (void)startCountdown{
    __block NSInteger timeOut = self.time;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(_timer, ^{
        if (timeOut <= 0) {
            // 结束
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self removeFromSuperview];
            });
        } else {
            int seconds = timeOut % 60;
            NSString *timeStr = [NSString stringWithFormat:@"%0.2d", seconds];
            if([timeStr isEqualToString:@"00"]){
                timeStr = @"60";
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.btnConfirm setTitle:[NSString stringWithFormat:@"OK(%@s)",timeStr] forState:UIControlStateNormal];
            });
            timeOut--;
        }
    });
    dispatch_resume(_timer);
}

#pragma mark - 刷新view
- (void)resetViewWithLeftMoney:(NSString *)leftMoney middleMoney:(NSString *)middleMoney rightMoney:(NSString *)rightMoney time:(NSInteger)time{
    self.time = time;
    
    self.imageBack.rightTop = XY(self.viewBack.width+self.viewBack.width/3-40, 0-self.viewBack.width/5-10);
    
    self.labelTitle.centerXTop = XY(self.viewBack.width/2, 20);
    self.imageBig.centerXTop = XY(self.viewBack.width/2, self.labelTitle.bottom+18);
    self.btnConfirm.centerXTop = XY(self.viewBack.width/2, self.imageBig.bottom+18);
    self.viewBack.height = self.btnConfirm.bottom+20;
    self.viewBack.centerXCenterY =  XY(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
    
    [self.labelMoneyMiddle fitTitle:[NSString stringWithFormat:@"$%@",[GlobalMethod showMoney:middleMoney]] variable:0];
    self.labelMoneyMiddle.centerXBottom = XY(self.imageBig.width/2, self.imageBig.height-20);
    
    [self.labelMoneyLeft fitTitle:[NSString stringWithFormat:@"$%@",[GlobalMethod showMoney:leftMoney]] variable:0];
    self.labelMoneyLeft.centerXBottom = XY(self.imageBig.width/6, self.imageBig.height-5);
    
    if(isStr(rightMoney)){
        [self.labelMoneyRight fitTitle:[NSString stringWithFormat:@"$%@",[GlobalMethod showMoney:rightMoney]] variable:0];
    }else{
        [self.labelMoneyRight fitTitle:@"?" variable:0];
    }
    
    self.labelMoneyRight.centerXBottom = XY(self.imageBig.width/6*5, self.imageBig.height-36);
    
    [self startCountdown];
}

@end
